<?php

if(isset($_POST['cuFilId']) && isset($_POST['toDirId']) && isset($UID)){
#if it is a dirAdd request

  $cuFilId = $_POST['cuFilId'];
  $toDirId = $_POST['toDirId'];

  require_once $BASE_PATH . 'm/fil.php';
  $filcn = new FIL(DBHOST, DBUSER, DBPASS, DBNAME);
  require_once $BASE_PATH . 'm/dir.php';
  $dircn = new DIR(DBHOST, DBUSER, DBPASS, DBNAME);
  $cuFil = $filcn->getById($cuFilId);
  $orDir = $dircn->getById($cuFil['parent']);
  $toDir = $dircn->getById($toDirId);

  if($cuFil === false){
    echo '{"code": 1, "message": "fil not exist"}';
  }else if($toDir === false){
    echo '{"code": 2, "message": "dir not exist"}';
  }else if($orDir['id'] === $toDir['id']){
    echo '{"code": 3, "message": "already there"}';
  }else if($cuFil['uid'] !== $toDir['uid']){
    echo '{"code": 4, "message": "not belong to"}';
  }else{
    $filcn->alt(
      $cuFil['id'],
      $toDir['id'],
      $cuFil['name'],
      $cuFil['link'],
      $cuFil['uid']
    );
    $dircn->alt(
      $orDir['id'],
      $orDir['parent'],
      $orDir['name'],
      $orDir['subdir'],
      $orDir['file'] - 1,
      $orDir['uid']
    );
    $dircn->alt(
      $toDir['id'],
      $toDir['parent'],
      $toDir['name'],
      $toDir['subdir'],
      $toDir['file'] + 1,
      $toDir['uid']
    );
    require_once $BASE_PATH . 'm/bookmark.php';
    $bmcn = new Bookmark(DBHOST, DBUSER, DBPASS, DBNAME);
    $bookmark = $bmcn->get($UID);
    echo '{"code": 0, "data":' . json_encode($bookmark) . '}';
  }
}